
package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.PageBean;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.FavoriteService;
import cn.itcast.travel.service.RouteService;
import cn.itcast.travel.service.impl.FavoriteServiceImp;
import cn.itcast.travel.service.impl.RouteServiceImp;
import cn.itcast.travel.util.JedisUtil;
import cn.itcast.travel.web.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/route/*")
public class RouteServlet extends BaseServlet {
    private RouteService routeService=new RouteServiceImp();
    private FavoriteService favoriteService=new FavoriteServiceImp();
    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String cidStr = request.getParameter("cid");
        String rname = request.getParameter("rname");
        if("null".equals(rname)){
            rname = "";
        }

        int cid=0;
        if ( (cidStr!=null||cidStr.length()>0) && !("null".equals(cidStr))&&!("".equals(cidStr))){
            cid=Integer.parseInt(cidStr);
        }else {
            cid=5;
        }
        int currentPage=0;
        if (currentPageStr==null||currentPageStr.length()==0){
            currentPage=1;
        }else {
            currentPage=Integer.parseInt(currentPageStr);
        }
        int pageSize=0;
        if (pageSizeStr==null||pageSizeStr.length()==0){
            pageSize=5;
        }else {
            pageSize=Integer.parseInt(pageSizeStr);
        }

        PageBean<Route> pageBean = routeService.pageQuery(cid, currentPage, pageSize,rname);

        writeValue(pageBean,response);
    }
    public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String rid = request.getParameter("rid");
         Route route=routeService.findOne(rid);
         writeValue(route,response);
    }
    public void isFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String rid = request.getParameter("rid");
        User user =(User) JedisUtil.getRedisObject(request.getSession().getId());
        int uid;

        if (user==null){
            uid=0;
        }else {
            uid=user.getUid();
        }
        boolean favorite = favoriteService.isFavorite(rid, uid);
            writeValue(favorite,response);
    }
    public void addFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String rid = request.getParameter("rid");
        User user =(User) JedisUtil.getRedisObject(request.getSession().getId());
        int uid;
        if (user==null){
            return;
        }else {
                uid=user.getUid();
        }
        favoriteService.addFavorite(rid,uid);
    }
    public void MyFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPageStr = request.getParameter("currentPage");
        int currentPage=0;
        if (currentPageStr==null||currentPageStr.length()==0){
            currentPage=1;
        }else {
            currentPage=Integer.parseInt(currentPageStr);
        }
        int pageSize=12;
        User login =(User)JedisUtil.getRedisObject(request.getSession().getId());
        PageBean<Route> myFavorite = favoriteService.findMyFavorite(currentPage, pageSize, login.getUid());
        writeValue(myFavorite,response);
    }
    public void FavoriteQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currentPageStr = request.getParameter("currentPage");
        String rname = request.getParameter("rname");
        String spriceStr = request.getParameter("Sprice");
        String epriceStr = request.getParameter("Eprice");
        int currentPage=0;
        if (currentPageStr==null||currentPageStr.length()==0){
            currentPage=1;
        }else {
            currentPage=Integer.parseInt(currentPageStr);
        }
        int sprice=0;
        if (spriceStr==null||spriceStr.length()==0||"".equals(spriceStr)){

        }else {
            sprice=Integer.parseInt(spriceStr);
        }
        int eprice=0;
        if (epriceStr==null||epriceStr.length()==0||"".equals(epriceStr)){

        }else {
            eprice=Integer.parseInt(epriceStr);
        }
        PageBean<Route> query=favoriteService.findFavoriteQuery(currentPage,rname,sprice,eprice);
            writeValue(query,response);

    }
    public void indexRoute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          PageBean<Route> query=routeService.findIndex();
          writeValue(query,response);
    }
}
